package com.syscom.server.listener;

import java.lang.management.ManagementFactory;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.syscom.qcRule.util.GetQCRulePathUtil;
import com.syscom.server.qc.cacheManager.AccuNBST4CacheManager;
import com.syscom.server.qc.cacheManager.AccuRule4CacheManager;
import com.syscom.server.qc.cacheManager.QCManager;
import com.syscom.server.qd.cacheManager.DrawBackThreadLockManager;
import com.syscom.server.qd.cacheManager.GlobalConfigManager;
import com.syscom.server.qd.cacheManager.RuleQtaCacheManager;
import com.syscom.safe.wac.ap.WacApListener;
import com.syscom.safe.wac.exception.WacException;

public class QDCApListener implements WacApListener {
	@SuppressWarnings("unused")
	private transient static final String mark = "10BCC85DDC54DDF27A18E78A2AA7EB50A27C9789E7CDBE9FD4DD9C9DC56D7247A585625B8AE77885";
	private static Logger mLogger = Logger.getLogger(QDCApListener.class.getName());

	@Override
	public void afterCleanupAp() {
	}

	@Override
	public void afterInitAp(WacException arg0) {
		
		initManager();
		
		mLogger.info("QD init Success!");
	}

	private void initManager() {
		initQDManager();
		initQCManager();
	}
	
	private void initQDManager() {
		// 1.RuleQtaCacheManager
		RuleQtaCacheManager.getInstance();
		// 2.GlobalConfigManager
		GlobalConfigManager.getInstance();
		// 3.RuleQtaCacheManager
		RuleQtaCacheManager.getInstance().initAfterGlobalConfig();
		// 4.DrawBackManager
		DrawBackThreadLockManager.getInstance();
	}
	
	private void initQCManager() {
		GetQCRulePathUtil.getInstance();
		AccuRule4CacheManager.getInstance();
		AccuNBST4CacheManager.getInstance();
		QCManager.getInstance();
	}

	@Override
	public void beforeCleanupAp() {
	}

	@Override
	public boolean beforeInitAp() {
		logProcessId();
		return true;
	}
	
	/**
	 * log Process Id
	 */
	private void logProcessId() {
		StringBuilder logBuffer = new StringBuilder();
		
		logBuffer.append("QD Pid=");
		logBuffer.append(ManagementFactory.getRuntimeMXBean().getName());
		
		mLogger.info(logBuffer.toString());
	}

	@Override
	public String getName() {
		return null;
	}

	@Override
	public boolean getTraceFlag() {
		return false;
	}

	@Override
	public void onSqlException(String arg0, PreparedStatement arg1, SQLException arg2) {
	}

	@Override
	public void onSystemHalted(Object arg0) {
	}

	@Override
	public void onSystemStatusChange(long arg0, String arg1, int arg2, String arg3) {
	}

	@Override
	public void setName(String arg0) {
	}

	@Override
	public void setTraceFlag(boolean arg0) {
	}
}
